package yl.hs.bmipfull.controllers.host;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.production.ProductionLineEntity;
import yl.hs.bmipfull.security.HostValidation;
import yl.hs.bmipfull.service.host.IHostRecordService;
import yl.hs.bmipfull.service.production.IProductionLineService;
import yl.hs.bmipfull.utils.DateUtil;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 张凯
 * @apiNote 生产消耗信息 -controller
 * @since 21-10-23 下午 1:34 创建
 */
@Api(value = "生产消耗信息Web接口", tags = "主机程序——生产消耗信息")
@RequestMapping("/host/record")
@RestController
public class HostRecordController extends ControllerBase {

    @Autowired
    IHostRecordService productionRecordService;

    @Autowired
    IProductionLineService lineService;


    @ApiOperation(value = "保存消耗信息", notes = "保存消耗信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult saveRecord(@RequestBody List<Map<String,String>> records) {
        ProductionLineEntity line = HostValidation.getLine();
        productionRecordService.saveProductRecord(records, line);
        return success("保存成功");
    }

    @ApiOperation(value = "获取最大时间", notes = "获取最大时间")
    @RequestMapping(value = "/maxTime", method = RequestMethod.GET)
    public WebResult getMaxTime(String type){
        ProductionLineEntity line = HostValidation.getLine();
            Date maxTime = productionRecordService.getMaxTime(line.getSn(),type);
        var format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return success("获取成功",format.format(maxTime));

    }

    @ApiOperation(value = "获取生产线指定时间范围内的每一天生产量和消耗总计", notes = "获取生产线指定时间范围内的每一天生产量和消耗总计")
    @RequestMapping(value = "/cmpData/{lineSN}/{start}/{end}", method = RequestMethod.POST)
    public WebResult queryCompareData(@ApiParam(value = "生产线编号") @PathVariable String lineSN,
                               @ApiParam(value = "开始时间") @PathVariable String start,
                               @ApiParam(value = "结束时间") @PathVariable String end){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return success("ok",productionRecordService.queryCompareData(lineSN,sdf.parse(start.indexOf(":")>0?start:(start + " "+"00:00:00")),
                    sdf.parse(end.indexOf(":")>0?end:(end + " "+"00:00:00"))));
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "保存消耗信息", notes = "保存消耗信息")
    @RequestMapping(value = "/sumData/{lineSN}/{start}/{end}", method = RequestMethod.POST)
    public WebResult querySumData(@ApiParam(value = "生产线编号") @PathVariable String lineSN,
                               @ApiParam(value = "开始时间") @PathVariable String start,
                               @ApiParam(value = "结束时间") @PathVariable String end){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return success("ok",productionRecordService.querySummaryData(lineSN,sdf.parse(start.indexOf(":")>0?start:(start + " "+"00:00:00")),
                    sdf.parse(end.indexOf(":")>0?end:(end + " "+"00:00:00"))));
        }
        catch (Exception ee){
            return error(ee);
        }
    }
}
